package org.farm.common.response;

import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.farm.common.response.enums.ResultCodeEnums;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2025/4/8
 * @description
 */
@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {


    @Serial
    private static final long serialVersionUID = 1L;

    private boolean success;

    private Integer code;

    private String message;

    private T data;

    /**
     *  Constructor of Result
      * @param data data
     * @return Result
     * @param <T> T
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setCode(ResultCodeEnums.SUCCESS.getCode());
        result.setMessage(ResultCodeEnums.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }



    public static <T> Result<T> fail(ResultCodeEnums resultCodeEnums, String message) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.FALSE);
        result.setCode(resultCodeEnums.getCode());
        if (StringUtils.isNotBlank(message)){
            result.setMessage(message);
        }else {
            result.setMessage(resultCodeEnums.getMessage());
        }
        return result;
    }

}
